$(document).ready(function() {	
	showOrderForm();
});

function showOrderForm() {
	//已下订单
	item_order1_list = $("#item_order1_form");
	item_order1_list.empty();
	$.ajax({
		type:"post",
		url:"orderFormAction!showAllBook.action",
		dataType:"json",
		success:function(result){
			//var result2=eval(result);//防止返回的不是json而是字符串
			//alert(result);
		    if(result == ""){
		    	
				item_order1_list.empty();
				item_order1_list.append('<p>无任何信息/p>');
				//alert("1111111111");
			}else{
				//alert("2222222222");
				e = '<thead>'+
				    '<tr>'+
				    '<th style="width:10%;text-align: center;">订单编号</th>'+
					'<th style="width:10%;text-align: center;">物品名称</th>'+
					'<th style="width:20%;text-align: center;">订单时间</th>'+
					'<th style="width:10%;text-align: center;">订单价格</th>'+
					//'<th style="width:10%;text-align: center;">订单状态</th>'+
					'<th style="width:25%;text-align: center;">商家回复</th>'+	
					'<th style="width:25%;text-align: center;">订单操作</th>'+			
				'</tr>'+
			    '</thead>';
				//var list = eval(result);
				var result1 = result.book;
				$.each(result1,function(){
					e +='<tbody align="center">' + '<tr>' +			
					    '<td id="orderId">'+this.orderId+'</td>'+
					    '<td id="orderId">'+this.item.itemName+'</td>'+
					    '<td id="orderDate">'+this.orderDate+'</td>'+								
						'<td id="orderPrice">'+this.orderPrice+'</td>'+	
						//'<td id="orderStatus">'+this.orderStatus+'</td>'+	
						'<td id="salerWords">'+this.salerWords+'</td>'+	
						'<td><button type="button" class="btn btn-primary" onclick="showOrderDetail('+this.orderId+');">评价</button></td>'+
					'</tr>' + '</tbody>';
				});
				item_order1_list.append(e);		
			}
			//重新渲染
		},
		error:function(err){			
			alert(err.message);
		}
	});  
	
	//收到的订单
	item_order2_list = $("#item_order2_form");
	item_order2_list.empty();
	//alert("aaaaaaa");
	$.ajax({
		type:"post",
		url:"orderFormAction!showAllReceive.action",
		dataType:"json",
		success:function(result){
			//var result2=eval(result);//防止返回的不是json而是字符串
			//alert(result);
		    if(result == ""){
				item_order2_list.empty();
				item_order2_list.append('<p>无任何信息/p>');
				//alert("1111111111");
			}else{
				//alert("2222222222");
				e = '<thead>'+
				    '<tr>'+
				'<th style="width:10%;text-align: center;">订单编号</th>'+
				'<th style="width:10%;text-align: center;">物品名称</th>'+
				'<th style="width:20%;text-align: center;">订单时间</th>'+
				'<th style="width:10%;text-align: center;">订单价格</th>'+
				//'<th style="width:10%;text-align: center;">订单状态</th>'+
				'<th style="width:25%;text-align: center;">商家回复</th>'+	
				'<th style="width:25%;text-align: center;">订单操作</th>'+		
				'</tr>'+
			    '</thead>';
				//var list = eval(result);
				var result1 = result.receive;
				$.each(result1,function(){
					e +='<tbody align="center">' + '<tr>' +			
					    '<td id="orderId">'+this.orderId+'</td>'+
					    '<td id="orderId">'+this.item.itemName+'</td>'+
					    '<td id="orderDate">'+this.orderDate+'</td>'+								
						'<td id="orderPrice">'+this.orderPrice+'</td>'+	
						//'<td id="orderStatus">'+this.orderStatus+'</td>'+	
						'<td id="salerWords">'+this.salerWords+'</td>';
					if(this.orderStatus==0){
						e+='<td><button id="caozuo1" type="button" class="btn btn-primary" style="margin-left: 1%;" onclick="confirm('+this.orderId+',1);">确认</button>'+
						'&nbsp;&nbsp;<button id="caozuo2" type="button" class="btn btn-primary" onclick="confirm('+this.orderId+',2);">婉拒</button></td>'+
					'</tr>' + '</tbody>';
					}
					else if(this.orderStatus==1){
						e+='<td>已确认</td>'+
						'</tr>' + '</tbody>';
					}
					else if(this.orderStatus==2){
						e+='<td>已拒绝</td>'+
						'</tr>' + '</tbody>';
					}
				});
				item_order2_list.append(e);		
			}
			//重新渲染
		},
		error:function(err){
			alert(err.message);
		}
	});
}
////////////////////////////////////////////////////////////////////////////
function showOrderDetail(orderId) {//显示订单详情
	
	 $("#item_name2").empty();
	 $("#order_price1").empty();
	 $("#item_number2").empty();
	 $("#item_description2").empty();
	 $("#item_owner2").empty();
	 $("#order_date1").empty();
	 $("#order_evaluate2").empty();
	 
	$.ajax({
		type:"post",
		url:"orderFormAction!findOrderByOrderId.action",
		data:{"orderId":orderId},
		dataType:"json",
		success:function(result){
		    if(result == ""){
				alert("1111111111");
			}else{
				//alert(result);	
				var result1 = result;
				$.each(result1,function(){
					
					 $("#orderId1").val(orderId);//赋值
					 $("#item_name2").val(this.item.itemName);
					 $("#order_price1").val(this.orderPrice);//赋值
					 $("#buy_num1").val(this.orderPrice/this.item.itemPrice);
					 $("#item_description2").val(this.item.itemDiscription);
					 $("#item_owner2").val(this.customer.customerName);
					 $("#order_date1").val(this.orderDate);
					 $("#order_evaluate2").val(this.evaluate);
					 
					 $("#orderId1").css("color","black");//赋值
					 $("#item_name2").css("color","black");//赋值
					 $("#order_price1").css("color","black");//赋值
					 $("#buy_num1").css("color","black");
					 $("#item_description2").css("color","black");
					 $("#item_owner2").css("color","black");
					 $("#order_date1").css("color","black");
					 $("#order_evaluate2").css("color","black");	
					 
				});
			}
		    $("#order_evaluate").click();	
		},
			//重新渲染
		error:function(err){		
			alert(err.message);
		}
	});			
	
	showOrderForm();
}
/////////////////////////////////////////////////////////////////////////////////
function evaluate_item() {//给订单加评价
	//alert("1");
	var orderId= $("#orderId1").val();
	var order_evaluate= $("#order_evaluate2").val();//取值
	//alert("评");
	$.ajax({
		type:"post",
		url:"orderFormAction!evaluate.action",
		data:{"orderId":orderId,"evaluate":order_evaluate},
		//dataType:"json",
	});
	alert("评价成功");
	$('#myModal1').modal('hide');
	showOrderForm();
}
/////////////////////////////////////////////////////////////////////////////////
function confirm(orderId,orderStatus) {//商家确认订单
	$.ajax({
		type:"post",
		url:"orderFormAction!updateOrderStatus.action",
		data:{"orderId":orderId,"orderStatus":orderStatus},
		//dataType:"json",
	});
	setTimeout("showItem();",1000);  //1秒后执行
	setTimeout("rentItem();",1000);
	setTimeout("bookItemManage();",1000);
	setTimeout("showOrderForm();",1000);
	
	
}